import router from '../router'
import axios from 'axios'
import Cookie from 'js-cookie'
import consts from './consts'
import getDeviceInfo from './deviceInfo'

// 在有catid的情况下进行登录
export const login = route => {
  let code = route.query.code
  let state = route.query.state
  let catid = route.query.catid || route.params.catid
  return new Promise((resolve, reject) => {
    // 不是手机端微信浏览器 直接返回
    if (!(getDeviceInfo().isWeiXin && getDeviceInfo().isPhone)) {
      resolve()
      return
    }
    if (!Cookie.get('ppz_openinfo') && !Cookie.get('cqv_openinfo') && catid) {
      // 没有登录状态
      if (code) {
        axios
          .post(consts.baseUrl + 'oauth_login.php?code=' + code + '&state=' + state + '&catid=' + catid)
          .then(data => {
            // 状态：
            // 1 获取成功，正常状态
            // 10 获取失败，code值已失效；
            // 20000 封号状态
            // 0 参数错误
            if (parseInt(data.data.status) === 1) {
              resolve()
            } else if (parseInt(data.data.status) === 20000) {
              reject()
              router.replace('/infoError/' + catid)
            } else {
              reject()
              let nowPageUrl = window.location.href
              // 去除 code 和 status
              let urlA = nowPageUrl.indexOf('&code=')
              let urlB = nowPageUrl.substring(0, urlA)
              urlB = encodeURIComponent(urlB)
              redirect(urlB)
            }
          })
          .catch(err => {
            reject(err)
          })
      } else {
        reject()
        let pageUrl = encodeURIComponent(window.location.href)
        redirect(pageUrl)
      }
    } else {
      resolve()
    }
  })
}

function redirect(urlp) {
  let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + consts.appId + '&redirect_uri=' + urlp + '&response_type=code&scope=snsapi_base&state=7da8ab58b#wechat_redirect'
  window.location.href = url
}

// 检查是否被封号

export const check = route => {
  let catid = route.query.catid || route.params.catid
  // let code = route.query.code
  // 在有登录状态和catid的情况下检查是否被封号
  return new Promise((resolve, reject) => {
    // 不是手机端微信浏览器 直接返回
    if (!(getDeviceInfo().isWeiXin && getDeviceInfo().isPhone)) {
      resolve()
      return
    }
    if ((Cookie.get('ppz_openinfo') || Cookie.get('cqv_openinfo')) && catid) {
      axios
        .post(consts.baseUrl + 'oauth_login.php?' + '&catid=' + catid)
        .then(data => {
          // 状态：
          // 1 获取成功，正常状态
          // 10 获取失败，code值已失效；
          // 20000 封号状态
          // 0 参数错误
          if (typeof data.data.status === 'undefined') {
            // 登录验证失败 清除cookie
            Cookie.remove('ppz_openinfo', {
              path: '/',
              domain: consts.cookieDomain
            })
            Cookie.remove('cqv_openinfo', {
              path: '/',
              domain: consts.cookieDomain
            })
            reject('请刷新页面')
            return
          }
          if (parseInt(data.data.status) === 20000) {
            reject()
            router.replace('/infoError/' + catid)
          } else {
            resolve()
          }
        })
        .catch(() => {
          reject()
        })
    } else {
      resolve()
    }
  })
}